package com.gitee.wsl.struct.grammar.ast.process.marker.block

import com.gitee.wsl.struct.grammar.ast.node.NodeType
import com.gitee.wsl.struct.grammar.ast.process.constraints.TextReadConstraints
import com.gitee.wsl.struct.grammar.ast.process.marker.ProductionHolder
import com.gitee.wsl.struct.grammar.ast.process.sequential.SequentialParser


abstract class InlineStructureHoldingMarkerBlock(
    constraints: TextReadConstraints,
    protected val productionHolder: ProductionHolder
)
: MarkerBlockImpl(constraints, productionHolder.mark()) {

    override fun acceptAction(action: MarkerBlock.ClosingAction): Boolean {
        if (action != MarkerBlock.ClosingAction.NOTHING) {
            if (action == MarkerBlock.ClosingAction.DONE
                    || action == MarkerBlock.ClosingAction.DEFAULT
                    && getDefaultAction() == MarkerBlock.ClosingAction.DONE
            ) {
                for (range in getRangesContainingInlineStructure()) {
                    productionHolder.addProduction(listOf(SequentialParser.Node(range, NodeType.ATX_1)))
                }
            }
        }

        return super.acceptAction(action)
    }

    abstract fun getRangesContainingInlineStructure(): Collection<IntRange>
}
